﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RVBike
{
    /// <summary>
    /// Interaction logic for LoadGameWindow.xaml
    /// </summary>
    public partial class LoadGameWindow : Window
    {
        public SaveState SelectedState { get; set; }

        public LoadGameWindow()
        {
            InitializeComponent();

            this.Title = MultiLanguage.Instance().Translate("load_game");

            btn_Load.Content = MultiLanguage.Instance().Translate("load");
            btn_Delete.Content = MultiLanguage.Instance().Translate("delete");
            btn_Cancel.Content = MultiLanguage.Instance().Translate("cancel");
        }

        private void RefreshList()
        {
            lb_SavedGame.Items.Clear();

            int saveCount = SaveManager.Instance().NumSaves();
            for (int i = 0; i < saveCount; ++i)
            {
                SaveState s = SaveManager.Instance().GetSave(i);
                lb_SavedGame.Items.Add(s);
            }
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            SelectedState = (SaveState)lb_SavedGame.SelectedItem;
            DialogResult = true;
            Close();
        }

        private void OnCancel(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void OnDelete(object sender, RoutedEventArgs e)
        {
            if (lb_SavedGame.SelectedIndex != -1)
            {
                SaveManager.Instance().RemoveSave(lb_SavedGame.SelectedIndex);
                RefreshList();
            }
        }

        private void OnSelectChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lb_SavedGame.SelectedIndex == -1)
            {
                btn_Load.IsEnabled = false;
            }
            else
            {
                btn_Load.IsEnabled = true;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshList();

            lb_SavedGame.SelectedIndex = -1;
            btn_Load.IsEnabled = false;
        }
    }
}
